/**
 * Project  : Looker
 * Package  : net.freestu.looker.impl
 * File     : MysqlConfigSynchronizer.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : Aug 22, 2009
 * Modified : Aug 22, 2009
 * License  : Apache License 2.0 
 */
package net.freestu.looker.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.freestu.looker.common.Category;
import net.freestu.looker.common.ConfigurationSynchronizer;
import net.freestu.looker.common.Site;
import net.freestu.looker.util.MysqlQuery;
/**
 *  
 */
public class MysqlConfigSynchronizer implements ConfigurationSynchronizer
{

	/* (non-Javadoc)
     * @see net.freestu.looker.common.ConfigurationSynchronizer#synchronize(java.util.List)
     */
    @Override
    public void synchronize(List<Site> sites) throws Exception
    {
    	Iterator<Site> it = sites.iterator();
    	while(it.hasNext())
    		synchronize(it.next());
    }
    
    

	/**
	 * 当前数据库中的站点hash
	 * 判断数据库是否存在，如果不存在，插入新纪录，存在更新记录
	 */
	private ArrayList <String> siteList;
	
	/**
	 * 当前数据库中的栏目hash
	 */
	private ArrayList <String> categoryList;
	
	
	/**
	 * 构造函数
	 */
	public MysqlConfigSynchronizer()
	{
		siteList     = new  ArrayList <String>();
		categoryList = new ArrayList <String>();
		queryConfig();
	}
	
	
	/**
	 * 同步数据
	 * @throws SQLException 
	 */
	public  void synchronize(Site site) throws SQLException
	{
		updateSiteConfig(site);
		updateCategoryConfig(site);
	}
	
	/**
	 * 查询数据库中的配置
	 */
	public void queryConfig()
	{
		try {
			//查询站点配置
			String sql = "SELECT sitehash FROM #site";
			ResultSet rs = MysqlQuery.getInstance().query(sql);
			while (rs.next())
				siteList.add(rs.getString("sitehash"));
			
			//查询栏目配置
			sql  = "SELECT categoryhash FROM #category";
			rs = MysqlQuery.getInstance().query(sql);
			while (rs.next())
				categoryList.add(rs.getString("categoryhash"));
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检查站点配置
	 * @throws SQLException 
	 */
	private void updateSiteConfig(Site site) throws SQLException
	{
		if ( siteList.contains( site.getHash())){
			//存在更新配置
		}else{
			//不存在，新增站点配置
			String sql = "INSERT INTO #site (sitehash,title,url) VALUES ("+
			site.getHash()+",'"+
			site.getTitle()+"','"+
			site.getUrl()+"');";
			MysqlQuery.getInstance().execute(sql);
		}	
	}
	
	
	/**
	 * 检查栏目配置
	 * @throws SQLException 
	 */
	private void updateCategoryConfig(Site site) throws SQLException
	{
		Iterator<Category> it = site.getCategoryList().iterator(); 
		Category cate         = null;  
		String siteHash       = site.getHash();
		while (it.hasNext()){
			cate = it.next();
			if ( categoryList.contains( cate.getHash())){
				//存在更新配置
			}else{
				//不存在，新增站点配置
				String sql = "INSERT INTO #category (categoryhash,sitehash,title,url) VALUES ("+
				cate.getHash()+","+
				siteHash+",'"+
				cate.getTitle()+"','"+
				cate.getPaging().getURL(1)+"');";
				MysqlQuery.getInstance().execute(sql);
			}	
			
		}
	}

}
